<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Illuminate\Support\Facades\DB;

/**
 * 排行榜
 *
 * @icon fa fa-circle-o
 */
class GameRanking extends Backend
{

    /**
     * GameRanking模型对象
     * @var \app\admin\model\GameRanking
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\GameRanking;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    //总排行榜
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = "coin";
            $list = $this->model
                ->where($where)
                ->field("player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order($sort, $order)
                ->group("player_id")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }

    //总排行榜
    public function times()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $model = new \app\admin\model\GameRankingInfo();
            $game_id = (int)$this->request->get('game_id', 0);

            $start_month = $this->request->get('start_month');
            if(empty($start_month)){
                $start_month = date("Y-m");
            }
            $end_month = $this->request->get('end_month');
            if(empty($end_month)){
                $end_month = date("Y-m");
            }
            $startTime = strtotime($start_month);
            $time = strtotime($end_month);
            $endTime = strtotime("+1 month",$time);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = "coin";
            $list = $model
                ->where($where)->where("unix_timestamp(`created_at`) >= ". $endTime." and unix_timestamp(`created_at`) < ".$startTime)
                ->field("game_id,game_name,count(*) as num,sum(coin) as coin,updated_at,from_unixtime(unix_timestamp(created_at), '%Y-%m') as mon")
                ->order($sort, $order)
                ->group("game_id,mon")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }



        return $this->view->fetch();

    }

    //总排行榜
    public function month()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $model = new \app\admin\model\GameRankingInfo();
            $game_id = (int)$this->request->get('game_id', 0);
            $startTime = time();
            $endTime = strtotime("-1 month",$startTime);


            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = "coin";
            $list = $model
                ->where($where)->where("unix_timestamp(`created_at`) >= ". $endTime." and unix_timestamp(`created_at`) < ".$startTime)
                ->field("player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order($sort, $order)
                ->group("player_id")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();


    }

    //总排行榜
    public function week()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $model = new \app\admin\model\GameRankingInfo();
            $game_id = (int)$this->request->get('game_id', 0);
            $startTime = time();
            $endTime = strtotime("-1 week",$startTime);



            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sort = "coin";
            $list = $model
                ->where($where)->where("unix_timestamp(`created_at`) >= ". $endTime." and unix_timestamp(`created_at`) < ".$startTime)
                ->field("player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order($sort, $order)
                ->group("player_id")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }

    //乒乓排行榜
    public function index1()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where("game_id",1)
                ->field("player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order("coin", "desc")
                ->group("player_id")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }

    //飞机排行榜
    public function index2()
    {

        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where("game_id",2)
                ->field("game_id,game_name,player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order("coin", "desc")
                ->group("player_id")
                ->paginate($limit);
            $sql = $this->model->getLastSql();

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }

    //农场排行榜
    public function index3()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where("game_id",3)
                ->field("game_id,game_name,player_id,player_name,player_avatar,sum(coin) as coin,updated_at")
                ->order("coin", "desc")
                ->group("player_id")
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }


}
